/**
 * Created by szhua on 2015/9/17.
 */

$(document).ready(function(){
    var $title=$("#title"),
        $note=$("#note"),
        $ul=$("#notesList"),
        li='<li><a href="#pgNotesDetail?title=LINK">ID</a></li> ',
        notesHdr='<li data-role="list-divider">Your Notes</li>',
        noNode='<li id="noNodes">You hava no notes</li>';
    var Notekeeper={};
    (function(app){
        var appName='Notekeeper',
            version='1.0';
        app.init=function(){
            var colors=app.colors();
            app.bindings();
            app.checkForStorage();
        };

        app.colors=function(){
            var colors=['red','blue','yellow','purple'];
            return colors;
        }
        app.bindings=function(){
            $('#btnAddNote').on('click',function(e){
                e.preventDefault();
                app.addNote($('#title').val(),$('#note').val());
            });
            $(document).on('click','#listItem a',function(e){
                e.preventDefault();
                var href=$(this)[0].href.match(/\?.*$/)[0];
                var title=href.replace(/^\?title=/,'');
                app.loadNote(title);
            })
            $(document).on('click',"#btnDelete",function(e){
                e.preventDefault();
                var key=$(this).data('href');
                app.deleteNode(key);
            })

        }
        app.deleteNode=function(key){
            var notesObj=app.getNotes();
            delete notesObj[key];
            localStorage['Notekeeper']=JSON.stringify(notesObj);
            //$.mobile.changePage('index.html');
            window.location.href="index.html";
            app.checkForStorage();
        }
        app.loadNote=function(title){
            var notes=app.getNotes(),
                note=notes[title],
                page=[
                    '<div data-role="page" data-url="details" data-add-backbtn="true" >',
                    '<div data-role="header">',
                    '<a href="index.html">back</a>',
                    '<h1>Notekeeper</h1>',
                    '<a id="btnDelete" data-href="ID" datarole="button" class="ui-btn-right">Delete</a> ',
                    '</div>',
                    '<div data-role="content"><h3>Title</h3><p>NOTE</p></div>',
                    '</div>'
                ].join('');
            var newPage=$(page);
            newPage.html(function(index,old){
                return old
                    .replace(/ID/g,title)
                    .replace(/Title/g ,title
                        .replace(/-/g,' '))
                    .replace(/NOTE/g,note)

            }).appendTo($.mobile.pageContainer);
            $.mobile.changePage(newPage);
        };
        app.addNote=function(title,note){
            if(title==''||note==''){
                return false;
            }
            var notes=localStorage['Notekeeper'];
            var notesObj;
            if(notes==undefined||notes==""){
                notesObj={};
            }else{
                notesObj=JSON.parse(notes);
            }
            notesObj[title.replace(/ /g,'-')]=note;
            console.log(notesObj);
            localStorage['Notekeeper']=JSON.stringify(notesObj);
            $("#title").val('');
            $("#note").val('');
            app.displayNotes();
        }
        app.getNotes=function(){
            var notes=localStorage['Notekeeper'];
            if(notes==undefined){
                return undefined;
            }else{
                return JSON.parse(notes);
            }
        }
        app.displayNotes=function(){
            var notesObj=app.getNotes();
            var listItem=$("#listItem");
            listItem.empty();
            for(var n in notesObj){
                listItem.append('<li id="noNodes"><a href="#pgNotesDetail?title='+n+'">'+n+'</a></li>').listview('refresh');;
            }
        }
        app.checkForStorage=function(){
            var notes=app.getNotes();
            if(!$.isEmptyObject(notes)){
                app.displayNotes();
            }else{
                $ul.html(notesHdr+noNode).listview('refresh');
            }
        }
        app.init();
    })(Notekeeper);


})

